package me.cxlr.qinlauncher2.view;

import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import me.cxlr.qinlauncher2.R;
import me.cxlr.qinlauncher2.constants.Constants;
import me.cxlr.qinlauncher2.util.ThemeUtil;
import me.cxlr.qinlauncher2.view.launcher.DpadKey;
import me.cxlr.qinlauncher2.view.launcher.FunctionKey;
import me.cxlr.qinlauncher2.view.launcher.NumberKey;

/**
 * @author 酷安用户:山轮纸
 */
public class LauncherActivity extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ThemeUtil.initTheme();
    }

    /*** 长按判断 */
    private boolean isLongPress = false;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 返回键
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        }
        // 菜单键
        else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        }
        // 数字键
        else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_0) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_1) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_2) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_3) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_4) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_5) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_6) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_7) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_8) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_9) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_STAR) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_POUND) {
            if (event.getRepeatCount() == 0) {
                event.startTracking();
            }
            return true;
        }
        // DPAD
        else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
            return DpadKey.getInstance().dpadKey(this, this, Constants.DPAD_KEY_UP);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
            return DpadKey.getInstance().dpadKey(this, this, Constants.DPAD_KEY_DOWN);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
            return DpadKey.getInstance().dpadKey(this, this, Constants.DPAD_KEY_LEFT);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
            return DpadKey.getInstance().dpadKey(this, this, Constants.DPAD_KEY_RIGHT);
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        // 返回键
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            isLongPress = true;
            return super.onKeyLongPress(keyCode, event);
        }
        // 菜单键
        else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
            isLongPress = true;
            return FunctionKey.getInstance().leftPhysicalFunctionKeyLongClick(this, this);
        }
        // 数字键
        else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_0) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_0, 4);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_1) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_1, 4);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_2) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_2, 4);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_3) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_3, 4);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_4) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_4, 4);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_5) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_5, 4);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_6) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_6, 4);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_7) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_7, 4);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_8) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_8, 4);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_9) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_9, 4);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_STAR) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_STAR, 4);
        } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_POUND) {
            isLongPress = true;
            return NumberKey.getInstance().numberKeyLongPress(this, this, Constants.NUMBER_KEY_LONG_POUND, 4);
        } else {
            return super.onKeyLongPress(keyCode, event);
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // 返回键
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (isLongPress) {
                isLongPress = false;
                return super.onKeyUp(keyCode, event);
            } else {
                return FunctionKey.getInstance().rightPhysicalFunctionKey(this, this);
            }
        }
        // 菜单键
        else if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
            if (isLongPress) {
                isLongPress = false;
                return super.onKeyUp(keyCode, event);
            } else {
                return FunctionKey.getInstance().leftPhysicalFunctionKey(this, this);
            }
        }
        // 数字键
        else if (event.getKeyCode() == KeyEvent.KEYCODE_0) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_0, 2);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_1) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_1, 2);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_2) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_2, 2);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_3) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_3, 2);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_4) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_4, 2);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_5) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_5, 2);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_6) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_6, 2);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_7) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_7, 2);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_8) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_8, 2);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_9) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_9, 2);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_STAR) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_STAR, 2);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_POUND) {
            if (isLongPress) {
                isLongPress = false;
                return true;
            } else {
                return NumberKey.getInstance().numberKey(this, this, Constants.NUMBER_KEY_POUND, 2);
            }
        } else {
            return super.onKeyUp(keyCode, event);
        }
    }
}
